<?php

namespace common\models\actor;

use common\models\app\AppDevice;
use Yii;
use common\db\ActiveRecord;

/**
 * This is the model class for table "{{%taglib}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $description 描述
 * @property string $thumb 缩略图/图标
 * @property string $scope 使用范围
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Taglib extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%taglib}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'description'], 'required'],
            [['created_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['description', 'thumb'], 'string', 'max' => 255],
            [['scope'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '技能名称',
            'description' => '技能描述',
            'thumb' => 'Thumb',
            'scope' => 'Scope',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getActorTag()
    {
        return $this->hasMany(ActorTag::class, ['taglib_id' => 'id']);
    }

    public function getActorUser()
    {
//        Yii::error($this->getActorTag()->debug(), 'xxxxxxxx');
        return $this->hasOne(Actor::class, ['id' => 'actor_id'])->via('actorTag');
    }

    public function getCheckedTag()
    {
//        Yii::error($this->getActorUser()->debug(), 'aaaaa');
        return $this->getActorUser()->andWhere(['user_id' => Yii::$app->user->id]);
    }

    public function getIsChecked()
    {
        return $this->checkedTag ? true : false;
    }

    public function fields()
    {
        return [
            'id',
            'name',
            'uid' => function () {
                return Yii::$app->user->id;
            },
            'check' => 'isChecked'
        ];
    }
}
